package com.sys.db.managerImpl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sys.db.dao.DefFlowDao;
import com.sys.db.dao.RunFlowJobDao;
import com.sys.db.manager.DefFlowManager;
import com.sys.pojo.DefFlow;

public class DefFlowManagerImpl implements DefFlowManager {
	private DefFlowDao defFlowDao;
	private RunFlowJobDao runFlowJobDao;
	public void delete(int id) throws Exception{
		
	}

	public List<DefFlow> findAll() throws Exception {
		
		List list=defFlowDao.findAll();
		return list;
	}
	/*
	 * 根据传来的json数组保存数据
	 * @see com.sys.db.manager.DefFlowManager#saveOrUpdate(java.lang.String)
	 */
	public void saveOrUpdate(String jsonString) throws Exception{
		JSONArray jsonArr=JSONArray.fromObject(jsonString);
		JSONObject jsonObj;
		DefFlow def;
		for(int i=0;i<jsonArr.size();i++){
			jsonObj=jsonArr.getJSONObject(i);
			def= (DefFlow) JSONObject.toBean(jsonObj,DefFlow.class);
			setNullToBlank(def);
			if(def.getFlowId()!=null){
				defFlowDao.update(def);
			}else{
				defFlowDao.save(def);
			}
		}
	}
	public void setNullToBlank(DefFlow defFlow){
		if(defFlow.getDependId()==null){
			defFlow.setDependId("");
		}
		if(defFlow.getRunHour()==null){
			defFlow.setRunHour("");
		}
		if(defFlow.getRunMinus()==null){
			defFlow.setRunMinus("");
		}
		if(defFlow.getStatus()==null){
			defFlow.setStatus("0");
		}
		if(defFlow.getWaitTimes()==null){
			defFlow.setWaitTimes(0);
		}
		if(defFlow.getAvgRun()==null){
			defFlow.setAvgRun(0);
		}
		if(defFlow.getAvgWait()==null){
			defFlow.setAvgWait(0);
		}
		if(defFlow.getRuntimes()==null){
			defFlow.setRuntimes(0);
		}
	}
	/*
	 * 分页查询
	 * @see com.sys.db.manager.DefFlowManager#findByPage()
	 */
	public List<DefFlow> findByPage(int start,int limit) throws Exception {
		
		List list=defFlowDao.findByPage(start, limit);
		return list;
	}
	public void deleteByIds(String ids)throws Exception{
		String[] id=ids.substring(0,ids.length()-1).split("@");
		for(int i=0;i<id.length;i++){
			defFlowDao.delete(new Integer(id[i]));
			runFlowJobDao.deleteByFlowId(new Integer(id[i]));
		}
	}
	public DefFlow findById(int id)throws Exception {
		DefFlow def=defFlowDao.findById(id);
		return def;
	}

	public void save(DefFlow defFlow) throws Exception{
		
		defFlowDao.save(defFlow);
	}

	public void update(DefFlow news)throws Exception {
		
		defFlowDao.update(news);
	}

	public DefFlowDao getDefFlowDao() {
		return defFlowDao;
	}

	public void setDefFlowDao(DefFlowDao defFlowDao) {
		this.defFlowDao = defFlowDao;
	}

	public RunFlowJobDao getRunFlowJobDao() {
		return runFlowJobDao;
	}

	public void setRunFlowJobDao(RunFlowJobDao runFlowJobDao) {
		this.runFlowJobDao = runFlowJobDao;
	}

}
